#!/bin/bash

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go mod tidy

GOPATH=$(go env GOPATH)

sudo mkdir -p /usr/local/etc/vms

# 首先下载依赖
echo "======================== download ==========================="
sudo apt install gcc libpcap-dev -y
go mod download
echo ""

echo "======================== compile and install vms ==========================="
go install ./
echo "vms install to $GOPATH/bin/vms"
echo ""

echo "======================== compile and install vmsd ==========================="
go install ./cmd/vmsd
echo "vmsd install to $GOPATH/bin/vmsd"
if [ -f $GOPATH/bin/vmsd ]; then
    sudo cp $GOPATH/bin/vmsd /usr/local/bin/vmsd
    echo "cp vmsd to /usr/local/bin/"
else
    echo "cp vmsd failed"
fi


echo ""

echo "======================== copy config file ==========================="
# 如果配置文件不存在，则将配置文件拷贝到指定目录下
if [ ! -f /usr/local/etc/vms/vpn.ini ]; then
    sudo cp vpn.ini /usr/local/etc/vms/vpn.ini
    sudo cp ./cache/key.cache /usr/local/etc/vms/key.cache
    sudo cp ./cache/sess.cache /usr/local/etc/vms/sess.cache
    echo "config file already copy to /usr/local/etc/vms/vpn.ini"
else
    echo "config file already exists~,rewrite the config file"
    sudo cp vpn.ini /usr/local/etc/vms/vpn.ini
    sudo cp ./cache/key.cache /usr/local/etc/vms/key.cache
    sudo cp ./cache/sess.cache /usr/local/etc/vms/sess.cache
fi
